<template>
  <div class="full">
    <div class="g-reg-wrap">
      <div class="m-reg-cet">
        <div class="m-reg-header">
          <p class="back">
            <router-link to="/">首页</router-link>
          </p>
          <p class="logo"><img src="../images/logo.png" width="147" height="76"/></p>
        </div>


        <!-- S 登录-->
        <div class="m-regbox">
          <div class="hd"></div>

          <div class="bd">
            <h2 class="reg-tit">登录</h2>
            <div class="m-formlist">
              <ul class="clearfix">
                <li>
                  <input v-model="mobile" class="int" type="text" name="" value="" placeholder="请输入手机号"/>
                </li>
                <li>
                  <input v-model="password" class="int" type="password" name="" value="" placeholder="请输入密码"/>
                </li>
              </ul>
              <p class="linkbtn clearfix">
                <router-link class="lb1" to="/register">立即注册</router-link>
                <router-link class="lb2" to="/forgetPassword">忘记密码？</router-link>
              </p>
              <p class="btn">
                <a v-on:click="login">登录</a>
              </p>
            </div>
          </div>
        </div>
        <!-- E 登录-->


      </div>

    </div>
  </div>
</template>

<script>
  import urlHelper from '@/lib/url_helper';

  export default {
    name: 'login',
    data() {
      return {
        mobile: '',
        password: '',
      }
    }, methods: {


      login() {
        let params = {
          'mobile': this.mobile,
          'password': this.password
        };
        this.$post('/user/login', params).then(response => {
          this.$check(response);

          let user = {
            'token': response.data.token
          };
          this.$store.dispatch('login', user);
          let userId = response.data.userId;
          this.$fetch('/user/getBaseUserInfo?userId=' + response.data.userId).then(response => {
            this.$check(response);
            let user = {
              'username': response.data.nickname,
              'photo': response.data.picUrl,
              'userId': userId,
            }
            this.$store.dispatch('updateUserInfo', user);
          });
          this.redirectToPage(userId);
        });
      },
      redirectToPage(userId) {
        let type = this.$route.query.type;
        let path = this.$route.query.path;
        let query = this.$route.query.query;
        let appendUserId = this.$route.query.appendUserId;
        if (!path || (type !== 'url' && type !== 'view')) {
          this.$router.replace({name: 'index'});
        } else {
          if (!query) {
            query = {};
          } else {
            query = urlHelper.parseUrlParam(query);
          }
          if (appendUserId) {
            query.userId = userId;
          }
          if (type === 'url') {
            window.location.replace(urlHelper.setUrlParams(query, path));
          } else {
            this.$router.replace({path: path, query: query});
          }
        }
      }
    }
  };
</script>

<style scoped>
  .full {
    background: #64abe1;
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
  }

</style>
